local rule = fk.CreateSkill {
  name = "#commanders_rule&",
}

-- 暖色方主帅初始手牌数+1
rule:addEffect(fk.DrawInitialCards, {
  priority = 0.001,
  can_refresh = function(self, event, target, player, data)
    return target == player and player.seat == 5
  end,
  on_refresh = function(self, event, target, player, data)
    print("commanders_rule effect triggered", event)
    data.num = data.num + 1
  end,
})

-- 回合结束时设置行动结束标记
rule:addEffect(fk.TurnEnd, {
  priority = 0.001,
  can_refresh = function(self, event, target, player, data)
    -- 对所有角色的回合结束均触发
    return target == player
  end,
  on_refresh = function(self, event, target, player, data)
    local room = player.room
    if not room then return end

    -- 确定当前角色属于哪个阵营（冷色方还是暖色方）
    local camp = player.role and string.sub(player.role, 1, 4) -- 获取"cool"或"warm"
    if not camp then return end

    -- 找到该阵营的主帅
    local marshal = table.find(room.players, function(p)
      return p and p.role and p.role == camp .. "_marshal"
    end)

    if marshal then
      -- 清除所有人的"上一个行动"标记
      for _, p in ipairs(room.alive_players) do
        if p then
          room:setPlayerMark(p, "@last_action", 0)
        end
      end
      -- 为当前阵营的主帅添加标记
      room:setPlayerMark(marshal, "@last_action", 1)

      -- 输出调试信息
      print("commanders_rule: set @last_action mark for " .. marshal.general)
    end
  end,
})

--主帅控制边锋
rule:addEffect(fk.GameStart, {
  priority = 0.001,
  can_refresh = function(self, event, target, player, data)
    return true
  end,
  on_refresh = function(self, event, target, player, data)
    local room = player.room
    if not room then return end

    print("commanders_rule effect triggered", event)
    -- 增加检查确保相关位置的玩家存在
    if room.players[2] and room.players[1] then
      room.players[2]:control(room.players[1])
    end
    if room.players[2] and room.players[3] then
      room.players[2]:control(room.players[3])
    end
    -- 5号位控制4、6号位
    if room.players[5] and room.players[4] then
      room.players[5]:control(room.players[4])
    end
    if room.players[5] and room.players[6] then
      room.players[5]:control(room.players[6])
    end
  end,
})

--AOE自选结算方向
rule:addEffect(fk.PreCardUse, {
  priority = 0.001,
  can_refresh = function(self, event, target, player, data)
    return target == player and data.card.multiple_targets and data.card.skill.min_target_num == 0
  end,
  on_refresh = function(self, event, target, player, data)
    if not player or not player.room then return end

    print("commanders_rule effect triggered", event)
    local choice = player.room:askToChoice(player, {
      choices = { "clockwise", "anticlockwise" },
      skill_name = "game_rule",
      prompt = "#commanders_aoe-choice:::" .. data.card:toLogString(),
    })
    if choice == "clockwise" then
      data.extra_data = data.extra_data or {}
      data.extra_data.commanders_reverse = true
    end
  end,
})
--AOE自选结算方向
rule:addEffect(fk.BeforeCardUseEffect, {
  priority = 0.001,
  can_refresh = function(self, event, target, player, data)
    return player and player.seat == 1 and
        data.extra_data and data.extra_data.commanders_reverse and #data.tos > 1
  end,
  on_refresh = function(self, event, target, player, data)
    if not player or not player.room then return end

    print("commanders_rule effect triggered", event)
    local room = player.room
    local new_tos = {}
    -- 增加空值检查
    if not room.current then return end

    local players = { room.current }
    table.insertTable(players, table.reverse(room:getOtherPlayers(room.current)))
    for _, p in ipairs(players) do
      for _, to in ipairs(data.tos) do
        -- 增加空值检查
        if p and to and to == p then
          table.insert(new_tos, p)
          break
        end
      end
    end
    data.tos = new_tos
  end,
})

--无中生有多摸一张牌（eg.孙乾转化出的原版无中），按理来说应该改卡牌的skill
rule:addEffect(fk.BeforeDrawCard, {
  priority = 0.001,
  can_refresh = function(self, event, target, player, data)
    if not player then return false end

    return target == player and data.skillName == "ex_nihilo_skill" and
        player:getFriends() and player:getEnemies() and #player:getFriends() < #player:getEnemies()
  end,
  on_refresh = function(self, event, target, player, data)
    print("commanders_rule effect triggered", event)
    data.skillName = "v33__ex_nihilo_skill"
    data.num = data.num + 1
  end,
})

return rule
